import ContextPool from "../../common/tool/context/ContextPool";
import LocalCommon from "../config/LocalCommon";
import MapBase from "./MapBase";
const { ccclass, property } = cc._decorator;
@ccclass
export default abstract class BarMap extends MapBase {

    @property
    public FoodNumber: number = 0;
    @property
    public BarNumber: number = 0;

    @property(cc.Prefab)
    public PrefabBar: cc.Prefab = null;
    @property(cc.Prefab)
    public PrefabFood: cc.Prefab = null;

    public init(content: ContextPool): boolean {
        super.init(content);
        this.createBar();
        this.createFood();
        return true;
    }

    public createBar() {
        let barPositonXList = this.getBarPositonXArray(this.BarNumber);
        for (let i = 0; i < this.BarNumber; i++) {
            let node = cc.instantiate(this.PrefabBar);

            // -- 最右边没意义
            node.position = cc.v3(barPositonXList[i] + LocalCommon.CubeWidth, 0);
            this.node.addChild(node);
        }
    }

    public getBarPositonXArray(number: number): Array<number> {
        number = Math.min(number, LocalCommon.MAX_ROW_CUBE - 1);


        let result: Array<number> = [];
        for (var i = 0; i < number; i++) {
            result.push(i * LocalCommon.CubeWidth);
        };
        this.shuffleSelf(result);
        // result.sort(function () { return 0.5 - Math.random() });
        return result;
    }

    public getFoodPositonXArray(number: number): Array<number> {
        number = Math.min(number, LocalCommon.MAX_ROW_CUBE);
        let result: Array<number> = [];
        for (let i = 0; i < number; i++) {
            result.push(i * LocalCommon.CubeWidth);
        };
        this.shuffleSelf(result);
        return result;
    }

    shuffleSelf(array: Array<any>): void {
        for (let i = array.length - 1; i > 0; i--) {
            const j = Math.floor(Math.random() * (i + 1));
            [array[i], array[j]] = [array[j], array[i]];
        }
    }

    public abstract createFood();
}
